package com.hx0311.pas.xccs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hx0311.core.dao.BaseDao;
import com.hx0311.core.dao.support.ResultSetUtil;
import com.hx0311.pas.xccs.model.Xccs;

public class XccsDao extends BaseDao {
	private Connection conn;
	private PreparedStatement stat;

	public void add(Xccs xccs) {
		if (xccs != null) {
			String sql = "insert into xccs (zh, xcrbh, xcbl, note) values (?, ?, ?, ?)";
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql);
				stat.setString(1, xccs.getZh());
				stat.setString(2, xccs.getXcrbh());
				stat.setFloat(3, xccs.getXcbl());
				stat.setString(4, xccs.getNote());
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				close(stat, conn);
			}
		}
	}

	public void update(Xccs xccs) {
		if (xccs != null) {
			String sql = "update xccs set xcbl = ?, note = ? where zh = ? and xcrbh = ?";
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql);
				stat.setFloat(1, xccs.getXcbl());
				stat.setString(2, xccs.getNote());
				stat.setString(3, xccs.getZh());
				stat.setString(4, xccs.getXcrbh());
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				close(stat, conn);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List getAll() {
		String sql = "select zh, xcrbh, xcbl, note from xccs";
		ResultSet rs = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			return ResultSetUtil.toList(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rs, stat, conn);
		}
		return java.util.Collections.EMPTY_LIST;
	}

	@Override
	public String getIdName() {
		return "zh";
	}

	@Override
	public String getTableName() {
		return "xccs";
	}

}
